package com.vadmin.vadminjava.service;


import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.common.JwtUtil;
import com.vadmin.vadminjava.common.ResultSetToJavaBean;
import com.vadmin.vadminjava.db.DBUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

/************************************************************************
*
 * 注意这里只放用户，管理，权限之类的逻辑
 *
* ************************************************************************/

@Component
public class AdminService {

    org.slf4j.Logger logger = LoggerFactory.getLogger(AdminService.class.getName());

    //用户登录
    public JSONObject userLogin(JSONObject request) {
        String     sql;
        DBUtil     dbUtil = new DBUtil();
        JSONObject map    = new JSONObject();
        map.put("resultFlag",false);
        map.put("resultMsg","失败");

        JSONObject dataMap = new JSONObject();
        logger.info("后台登陆：{}",request);

        //参数
        String _username = request.getString("username");
        String _password = request.getString("password");

        JSONObject _jwt = new JSONObject();
        _jwt.put("username",_username);
        _jwt.put("password",_password);

        try {

            sql = "select * from admin_user where account='" + _username + "' and password=md5('" + _password + "') ";
            ArrayList<JSONObject> list = ResultSetToJavaBean.copyJson(dbUtil.executeQuery(sql));
            if (list.size() > 0) {
                map.put("resultFlag",true);
                map.put("resultMsg","登录成功");
                _jwt.put("expiresTime",getTimesMillisIncrease(30));
                dataMap.put("token", JwtUtil.sign(_jwt.toJSONString()));

                //菜单权限
                JSONObject admin = list.get(0);
                dataMap.put("userName",_username);
                dataMap.put("id", admin.getInteger("id"));
                dataMap.put("purview",admin.getString("purview"));
                dataMap.put("welcomePage",admin.getString("welcomePage"));
                map.put("data",dataMap);
                return map;
            }
            throw new RuntimeException("账号或密码错误");

        } catch (RuntimeException ex) {
            map.put("resultFlag",false);
            map.put("resultMsg",ex.getMessage());
            return map;
        } catch (Exception ex) {
            logger.error("登录过程出错了:{}",ex);
        } finally {
            try {
                dbUtil.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return map;
    }


    //当前时间增加X天
    public String getTimesMillisIncrease(Integer days){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        //System.out.println("当前日期:"+sf.format(c.getTime()));
        c.add(Calendar.DAY_OF_MONTH, days); //增加天数
        String _ret = String.valueOf(c.getTimeInMillis());
        //System.out.println("增加一天后日期:"+sf.format(c.getTime()));
        //System.out.println("转成timestamp:"+ _ret);
        return _ret;
    }

}
